package com.lr.app.config;

import java.sql.SQLException;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

import com.alibaba.druid.pool.DruidDataSource;

@Configuration
public class ApplicationConfig {
	@Autowired
	private Environment env;

	private static String exStoragePath;

	public static String getExStoragePath() {
		return ApplicationConfig.exStoragePath;
	}

	@Bean
	public DataSource datasource() throws SQLException {
		DruidDataSource dataSource = new DruidDataSource();
		String username = env.getProperty("spring.datasource.username");
		String password = env.getProperty("spring.datasource.password");
		dataSource.setUrl(env.getProperty("spring.datasource.url"));
		dataSource.setUsername(username);
		dataSource.setPassword(password);
		// dataSource.setUsername(CipherUtils.decrypt(username));
		// dataSource.setPassword(CipherUtils.decrypt(password));
		ApplicationConfig.exStoragePath = env.getProperty("exStoragePath");
		if (ApplicationConfig.exStoragePath == null || ApplicationConfig.exStoragePath.trim().length() == 0) {
			ApplicationConfig.exStoragePath = "../circular-ex-storage";
		}
		dataSource.getConnection();
		return dataSource;
	}

}
